/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.domain.IsfdCollectBatchHistory;
import com.comac.ins.isfd.domain.bo.IsfdCollectBatchHistoryBo;
import com.comac.ins.isfd.domain.vo.IsfdCollectBatchHistoryVo;
import com.comac.ins.isfd.mapper.IsfdCollectBatchHistoryMapper;
import com.comac.ins.isfd.service.IIsfdCollectBatchHistoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 机型批次历史记录-采集Service业务层处理
 *
 * @author hxloongs
 * @date 2025-01-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class IsfdCollectBatchHistoryServiceImpl implements IIsfdCollectBatchHistoryService {

    @Autowired
    private IsfdCollectBatchHistoryMapper baseMapper;

    @Override
    public IsfdCollectBatchHistoryVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public TableDataInfo<IsfdCollectBatchHistoryVo> queryPageList(IsfdCollectBatchHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IsfdCollectBatchHistory> lqw = buildQueryWrapper(bo);
        Page<IsfdCollectBatchHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<IsfdCollectBatchHistoryVo> queryList(IsfdCollectBatchHistoryBo bo) {
        LambdaQueryWrapper<IsfdCollectBatchHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IsfdCollectBatchHistory> buildQueryWrapper(IsfdCollectBatchHistoryBo bo) {
        LambdaQueryWrapper<IsfdCollectBatchHistory> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getModelNumber()), IsfdCollectBatchHistory::getModelNumber, bo.getModelNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNumber()), IsfdCollectBatchHistory::getBatchNumber, bo.getBatchNumber());
        lqw.like(StringUtils.isNotBlank(bo.getBatchName()), IsfdCollectBatchHistory::getBatchName, bo.getBatchName());
        return lqw;
    }

    @Override
    public List<String> batchHistoryList(String modelNumber) {
        return baseMapper.selectBatchNumbersByModelNumber(modelNumber);
    }

    @Override
    @Transactional
    public void save(String modelNumber, String batchNumber) {
        IsfdCollectBatchHistoryBo bo = new IsfdCollectBatchHistoryBo();
        bo.setModelNumber(modelNumber);
        bo.setBatchNumber(batchNumber);
        List<IsfdCollectBatchHistoryVo> batchHistoryVoList = this.queryList(bo);
        if (!CollectionUtils.isEmpty(batchHistoryVoList)) {
            return;
        }
        IsfdCollectBatchHistory po = new IsfdCollectBatchHistory();
        po.setModelNumber(modelNumber);
        po.setBatchNumber(batchNumber);
        po.setBatchName(batchNumber);
        baseMapper.insert(po);
    }
}
